<?php
/**
 * 分销接口
 * User: 陈立强
 * Date: 2019/11/8
 * Time: 11:39
 */


namespace app\api\controller\v1;

use app\facade\Distribution as DictionaryFacade;//分销静态处理层


class Distribution extends Base

{
    /*
     * 分销商提交申请
     *
     * */
    public function distribution_apply(){

        $post = input('param.');
        $post['userid'] = $this->checkUserToken();
        if(empty($post['nickname'])){
            return j(1,'姓名不能为空');
        }
        if(empty($post['mobile'])){
            return j(1,'手机号不能为空');
        }
        try {
            $result = DictionaryFacade::distribution_apply($post);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /*
     * 我的下级
     * */
    public function distribution_member(){
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        $type = input('type',0);
        try {
            $result = DictionaryFacade::distribution_member($userId,$type,$limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }

    }


    /*
     * 分销订单
     *
     * */
    public function distribution_order(){
        $userId = $this->checkUserToken();
        $type = input('type', 0);
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            if($type == 0){
                $userlist = \app\common\model\Members::where('parent_id',$userId)->field('GROUP_CONCAT(id) as id')->select();
            }else{
                $userlist = \app\common\model\Members::where('parent_id',$userId)->field('GROUP_CONCAT(id) as id')->select();

                if($userlist){
                    $userlist = \app\common\model\Members::where('parent_id','in',$userlist[0]['id'])->field('GROUP_CONCAT(id) as id')->select();
                }
            }
            $result = DictionaryFacade::distribution_order($userlist,$limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /*
    * 佣金记录
    *
    * */
    public function distribution_list(){

        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            $result = DictionaryFacade::distribution_list($userId,$limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /*
     * 佣金提现
     *
     * */
    public function distribution_cash(){
        $post = input('post.');

        $post['userid'] = $this->checkUserToken();
        try {
            $result = DictionaryFacade::distribution_cash($post);

            return j(0, '提现成功');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }

    }

    /*
     * 佣金提现记录
     *
     * */
    public function distribution_record(){
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            $result = DictionaryFacade::distribution_record($userId,$limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }

    }

    /*
     * 分销海报
     *
     * */
    public function distribution_posters(){

        $userId = $this->checkUserToken();
        try {
            $result = DictionaryFacade::distribution_posters($userId);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }

    }


    /*
     * 分销文字
     *
     * */
    public function distribution_text(){

        $userId = $this->checkUserToken();
        $type = input('type',0);
        try {
            $result = DictionaryFacade::distribution_text($type);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }

    }

    /*
    * 分销奖励
    *
    * */
    public function distribution_reward()
    {
        $userId = $this->checkUserToken();
        try {
            $member = \app\facade\Members::info($userId);
            $data['commission'] = $member['commission'];
            $data['money'] = $member['money'];
            $data['distribution_num'] = $member['money'];
            return j(0, '获取数据成功', $data);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }


    /*
    * 分销排行
    *
    * */
    public function distribution_ranking()
    {
        $userId = $this->checkUserToken();
        try {
            $result = DictionaryFacade::distribution_ranking($userId);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }


}

